package cn.calm.user;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.apache.log4j.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;

import cn.calm.commons.dao.IBaseDao;
import cn.calm.entity.Menu;
import cn.calm.entity.User;
import cn.calm.entity.UserMenuApplication;
import cn.calm.user.service.IMenuService;
import cn.calm.user.service.IUserMenuApplicationService;
import cn.calm.user.service.IUserService;
import cn.calm.user.service.impl.MenuServiceImpl;
import cn.calm.user.service.impl.UserMenuApplicationServiceImpl;
import cn.calm.user.service.impl.UserServiceImpl;

@SuppressWarnings("rawtypes")
public class Activator implements BundleActivator {
	public static BundleContext context;
	private static Logger log = Logger.getLogger(Activator.class);

	private ServiceRegistration<IMenuService> menuRegistration;
	private ServiceRegistration<IUserService> userServiceRegistration;
	private ServiceRegistration<IUserMenuApplicationService> userMenuApplicationServiceRegistration;
	private EntityManager em;
	private EntityManagerFactory emf;

	@Override
	public void start(BundleContext context) throws Exception {
		log.debug("starting");
		try {
			Activator.context = context;
			ServiceReference<IBaseDao> serviceRef = context
					.getServiceReference(IBaseDao.class);
			IBaseDao dao = context.getService(serviceRef);

			log.debug("dao:" + dao);
			IUserService<User> userService = new UserServiceImpl<User>();
			emf = Persistence.createEntityManagerFactory("entity");
			em = emf.createEntityManager();
			// log.debug("em:" + em);
			dao.setSession(em);
			userService.setBaseDao(dao);

			userServiceRegistration = context.registerService(
					IUserService.class, userService, null);
			// log.debug("registerService success");
			IUserMenuApplicationService<UserMenuApplication> userMenuApplicationService = new UserMenuApplicationServiceImpl<UserMenuApplication>();
			// serviceRef = context.getServiceReference(IBaseDao.class);
			// dao = context.getService(serviceRef);
			userMenuApplicationService.setBaseDao(dao);
			// em = Persistence.createEntityManagerFactory("entity")
			// .createEntityManager();
			// dao.setSession(em);

			userMenuApplicationServiceRegistration = context.registerService(
					IUserMenuApplicationService.class,
					userMenuApplicationService, null);

			IMenuService<Menu> menuService = new MenuServiceImpl<Menu>();

			// serviceRef = context.getServiceReference(IBaseDao.class);
			// dao = context.getService(serviceRef);
			menuService.setBaseDao(dao);
			// em = Persistence.createEntityManagerFactory("entity")
			// .createEntityManager();
			dao.setSession(em);

			// registration =
			// bc.registerService(TemperatureService.class.getName(),
			// new TemperatureServiceImpl(), props);
			menuRegistration = context.registerService(IMenuService.class,
					menuService, null);
		
			
		} finally {
			log.debug("started");
		}
	}

	@Override
	public void stop(BundleContext arg0) throws Exception {
		Activator.context = null;
		if (userServiceRegistration != null) {
			userServiceRegistration.unregister();
		}
		if (userMenuApplicationServiceRegistration != null) {
			userMenuApplicationServiceRegistration.unregister();
		}
		if (menuRegistration != null) {
			menuRegistration.unregister();
		}
		if (em != null) {
			em.close();
		}
		if (emf != null) {
			emf.close();
		}
	}

}
